<?php


namespace app\widgets;


use app\models\Book;
use app\models\Comment;
use yii\base\Widget;
use yii\data\ActiveDataProvider;

class CommentWidget extends Widget {

    /* @var $model Book */
    public $model;
    public $limit = 10;

    public function run() {
        $book = $this->model;
        $query = Comment::find();
        $query->where(['book_id' => $book->id, 'type' => Comment::TYPE_BOOK]);
        $query->with(['user']);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [ 'defaultOrder' => ['id' => SORT_DESC]],
            'pagination' => $this->limit === FALSE ? FALSE : ['defaultPageSize' => $this->limit],
        ]);
        return $this->render('/comment', compact('dataProvider', 'book'));
    }
}